package com.jiutong.meipenggang.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.jiutong.meipenggang.R;
import com.jiutong.meipenggang.common.Constants;
import com.jiutong.meipenggang.http.model.interfaces.CheckLoginInterface;
import com.jiutong.meipenggang.ui.activitys.LoginActivity;
import com.jiutong.meipenggang.ui.activitys.LoginedActivity;
import com.jiutong.meipenggang.utils.SpConfig;
import com.jiutong.meipenggang.views.dialog.BaseDialog;

import butterknife.ButterKnife;

/**
 * @author Veken
 */
public abstract class BaseFragment extends Fragment {


    /**
     * 贴附的activity
     */
    protected FragmentActivity mActivity;

    /**
     * 根view
     */
    protected View mRootView;

    /**
     * 是否对用户可见
     */
    protected boolean mIsVisible;
    /**
     * 是否加载完成
     * 当执行完oncreatview,View的初始化方法后方法后即为true
     */
    protected boolean mIsPrepare;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        mActivity = getActivity();
    }

    @Override
    @Nullable
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(getLayoutResouceId(), container, false);
        ButterKnife.bind(this, mRootView);
        initData(getArguments());

        initView();

        mIsPrepare = true;


        initEvent();

        return mRootView;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    /**
     * 初始化数据
     *
     * @param arguments 接收到的从其他地方传递过来的参数
     */
    protected void initData(Bundle arguments) {

    }

    /**
     * 初始化View
     */
    protected void initView() {

    }

    /**
     * 设置监听事件
     */
    protected void initEvent() {

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        this.mIsVisible = isVisibleToUser;

        if (isVisibleToUser) {
            onVisibleToUser();
        }
    }

    /**
     * 用户可见时执行的操作
     */
    protected void onVisibleToUser() {
        if (mIsPrepare && mIsVisible) {
            onLazyLoad();
        }
    }

    /**
     * 懒加载，仅当用户可见切view初始化结束后才会执行
     */
    protected void onLazyLoad() {
        new Thread() {
            @Override
            public void run() {

            }
        }.start();

    }

    @SuppressWarnings("unchecked")
    protected <T extends View> T findViewById(int id) {
        if (mRootView == null) {
            return null;
        }

        return (T) mRootView.findViewById(id);
    }

    /**
     * 设置根布局资源id
     *
     * @return
     */
    protected abstract int getLayoutResouceId();

    public void openActivity(Class<?> pClass) {
        openActivity(pClass, null);
    }

    /**
     * 通过类名启动Activity，并且含有Bundle数据
     *
     * @param pClass
     * @param pBundle
     */
    public void openActivity(Class<?> pClass, Bundle pBundle) {
        Intent intent = new Intent(mActivity, pClass);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }

        if (pClass.isAnnotationPresent(CheckLoginInterface.class)) {

            if (TextUtils.isEmpty(SpConfig.getInstance().getString(Constants.SESSIONID_STRING))) {
                intent = new Intent(getActivity(), LoginActivity.class);
            }
        }

        startActivity(intent);
    }

    /**
     * 判断是否登录
     *
     * @return
     */
    protected boolean isLogin() {
        if (TextUtils.isEmpty(SpConfig.getInstance().getString(Constants.USERNAME_STRING))) {
            openActivity(LoginActivity.class);
            return false;
        } else if (!TextUtils.isEmpty(SpConfig.getInstance().getString(Constants.USERNAME_STRING))
                && !SpConfig.getInstance().getBool(Constants.IS_LOGIN)) {
            openActivity(LoginedActivity.class);
            return false;
        }
        return true;
    }

    /**
     * Toast提示
     *
     * @param str
     */
    public void toastShortShow(String str) {
        Toast.makeText(mActivity, str, Toast.LENGTH_SHORT).show();
    }

    public void toastLongShow(String str) {
        Toast.makeText(mActivity, str, Toast.LENGTH_LONG).show();
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
